import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_yishop/pages/shopcar/CartNum.dart';
import 'package:flutter_yishop/provider/Cart.dart';
import 'package:flutter_yishop/services/CartServices.dart';
import 'package:flutter_yishop/utils/screen/ScreenAdapter.dart';
import 'package:provider/provider.dart';

class CartItem extends StatefulWidget {
  Map _itemData;

  CartItem(this._itemData, {Key key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _CartItemState();
  }
}

class _CartItemState extends State<CartItem> {
  Map _itemData;
  var cartProvider ;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    ScreenAdapter.init(context);
    cartProvider = Provider.of<Cart>(context);
    this._itemData = widget._itemData;
    return Container(
      // height: ScreenAdapter.height(200),
      padding: EdgeInsets.all(5),
      decoration: BoxDecoration(
        border: Border(bottom: BorderSide(width: 1, color: Colors.black12)),
      ),
      child: Row(
        children: [
          Container(
            width: ScreenAdapter.width(60),
            child: Checkbox(
              value: _itemData[CartServices.checked],
              onChanged: (val) {
                setState(() {
                  _itemData[CartServices.checked] = !_itemData[CartServices.checked];
                });
                cartProvider.itemChange();
              },
              activeColor: Colors.pink,
            ),
          ),
          Container(
            width: ScreenAdapter.width(160),
            child: Image.network("${_itemData[CartServices.pic]}",
                fit: BoxFit.cover),
          ),
          Expanded(
              flex: 1,
              child: Container(
                padding: EdgeInsets.fromLTRB(10, 10, 10, 5),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Align(
                      alignment: Alignment.centerLeft,
                      child:
                          Text("${_itemData[CartServices.title]}", maxLines: 2),
                    ),
                    Align(
                      alignment: Alignment.centerLeft,
                      child: Text(
                        "${_itemData[CartServices.selectedAttr]}",
                        maxLines: 2,
                        style: TextStyle(fontSize: 12, color: Colors.black45),
                      ),
                    ),
                    Stack(
                      children: <Widget>[
                        Align(
                          alignment: Alignment.centerLeft,
                          child: Text("￥${_itemData[CartServices.price]}",
                              style: TextStyle(color: Colors.red)),
                        ),
                        Align(
                          alignment: Alignment.centerRight,
                          child: CartNum(_itemData),
                        )
                      ],
                    ),
                  ],
                ),
              )),
        ],
      ),
    );
  }
}
